
//拍照  
function getImage(picId) {  
    var cmr = plus.camera.getCamera();  
    cmr.captureImage(function(p) {  
        plus.io.resolveLocalFileSystemURL(p, function(entry) {  
            var localurl = entry.toLocalURL(); //  
            saveImg(localurl,picId);
        });  
    });  
}  
//相册选取  
function galleryImgs(picId) {  
    plus.gallery.pick(function(e) {  
        saveImg(e,picId);
    }, function(e) {  
    }, {  
        filter: "image"  
    });  
}  
//显示选择图片
function showActionSheet(picId) {  
    var bts = [{  
        title: "拍照"  
    }, {  
        title: "从相册选择"  
    }];  
    plus.nativeUI.actionSheet({  
            cancel: "取消",  
            buttons: bts  
        },  
        function(e) {  
            if (e.index == 1) {  
                getImage(picId);  
            } else if (e.index == 2) {  
                galleryImgs(picId);  
            }  
        }  
    );  
}  

function saveImg(url,picId){
	document.getElementById(picId).src=url;
	uploadHead(url);
}

//上传头像图片 
        function uploadHead(imgPath) { 
            console.log("imgPath = " + imgPath); 
            var image = new Image(); 
            image.src = imgPath; 
            image.onload = function() { 
                var imgData = getBase64Image(image); 
                /*在这里调用上传接口*/ 
//              mui.ajax("图片上传接口", { 
//                  data: { 
//                       
//                  }, 
//                  dataType: 'json', 
//                  type: 'post', 
//                  timeout: 10000, 
//                  success: function(data) { 
//                      console.log('上传成功'); 
//                  }, 
//                  error: function(xhr, type, errorThrown) { 
//                      mui.toast('网络异常，请稍后再试！'); 
//                  } 
//              }); 
            } 
    } 
        //将图片压缩转成base64 
        function getBase64Image(img) { 
            var canvas = document.createElement("canvas"); 
            var width = img.width; 
            var height = img.height; 
            // calculate the width and height, constraining the proportions 
            if (width > height) { 
                if (width > 100) { 
                    height = Math.round(height *= 100 / width); 
                    width = 100; 
                } 
            } else { 
                if (height > 100) { 
                    width = Math.round(width *= 100 / height); 
                    height = 100; 
                } 
            } 
            canvas.width = width;   /*设置新的图片的宽度*/ 
            canvas.height = height; /*设置新的图片的长度*/ 
            var ctx = canvas.getContext("2d"); 
            ctx.drawImage(img, 0, 0, width, height); /*绘图*/ 
            var dataURL = canvas.toDataURL("image/png", 0.8); 
            return dataURL.replace("data:image/png;base64,", ""); 
        }    